var CN_Date = require('getCNDate.js');

function getMonthFirstDay(m, y) {
  var t = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(1);
  return t.getDay();
}

function getMonthTotalDate(m, y) {
  var t = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(0);
  return t.getDate();
}

function getFromTodayDays(d, m, y) {
  var ret;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  var days = Math.floor((t.getTime() - t1.getTime()) / (24 * 60 * 60 * 1000)) || 0;
  // console.log(days)
  var ret = '今天';
  if (days == 0) {
    ret = '今天';
  } else if (days == 1) {
    ret = '明天';
  } else if (days == -1) {
    ret = '昨天';
  } else if (days > 1) {
    ret = days + '天后';
  } else if (days < -1) {
    ret = 0 - days + '天前';
  }
  return ret;
}

// 排班
function webScheduling(d, m, y) {
  // console.log(d, m, y)
  var name;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  // 获取格式为yyyy/mm/dd格式的日期
  var mytime = t.toLocaleDateString();
  // console.log(mytime)
  // 将日期转换成今年的第几天
  var dayiy = Math.ceil((new Date(mytime) - new Date(new Date(mytime).getFullYear().toString())) / (24 * 60 * 60 * 1000) + 1)
  // console.log(dayiy)
  switch (dayiy % 3) {
    case 1:
      name = "zh";
      break;
    case 2:
      name = "zxm";
      break;
    case 0:
      name = "sjj";
      break;
  }
  return name;
}

function javaScheduling(d, m, y) {
  var ret;
  var name;
  var date;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  var mytime = t.toLocaleDateString();
  var dayiy = Math.ceil((new Date(mytime) - new Date(new Date(mytime).getFullYear().toString())) / (24 * 60 * 60 * 1000) + 1)
  switch (dayiy % 5) {
    case 0:
      name = "lpy";
      break;
    case 1:
      name = "lh";
      break;
    case 2:
      name = "fy";
      break;
    case 3:
      name = "qg";
      break;
    case 4:
      name = "wsq";
      break;
  }
  return name;
}

function androidScheduling(d, m, y) {
  var ret;
  var name;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  // 获取格式为yyyy/mm/dd格式的日期
  var mytime = t.toLocaleDateString();
  var dayiy = Math.ceil((new Date(mytime) - new Date(new Date(mytime).getFullYear().toString())) / (24 * 60 * 60 * 1000) + 1)
  // console.log(dayiy % 2)
  switch (dayiy % 2) {
    case 1:
      name = "wjx";
      break;
    case 0:
      name = "cch";
      break;
  }
  return name;
}

function iosScheduling(d, m, y) {
  var ret;
  var name;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  var mytime = t.toLocaleDateString();
  var dayiy = Math.ceil((new Date(mytime) - new Date(new Date(mytime).getFullYear().toString())) / (24 * 60 * 60 * 1000) + 1)
  switch (dayiy % 2) {
    case 1:
      name = "ym";
      break;
    case 0:
      name = "lz";
      break;
  }
  return name;
}

function testScheduling(d, m, y) {
  var ret;
  var name;
  var date;
  var t = new Date();
  var t1 = new Date();
  t.setFullYear(y);
  t.setMonth(m);
  t.setDate(d);
  var mytime = t.toLocaleDateString();
  var dayiy = Math.ceil((new Date(mytime) - new Date(new Date(mytime).getFullYear().toString())) / (24 * 60 * 60 * 1000) + 1)
  // console.log(dayiy % 28)
  switch (dayiy % 28) {
    case 0:
      name = "sy";
      break;
    case 1:
      name = "ly";
      break;
    case 2:
      name = "zgl";
      break;
    case 3:
      name = "xc";
      break;
    case 4:
      name = "zgl";
      break;
    case 5:
      name = "zgl";
      break;
    case 6:
      name = "zgl";
      break;
    case 7:
      name = "sy";
      break;
    case 8:
      name = "ly";
      break;
    case 9:
      name = "zgl";
      break;
    case 10:
      name = "xc";
      break;
    case 11:
      name = "xc";
      break;
    case 12:
      name = "xc";
      break;
    case 13:
      name = "xc";
      break;
    case 14:
      name = "sy";
      break;
    case 15:
      name = "ly";
      break;
    case 16:
      name = "zgl";
      break;
    case 17:
      name = "xc";
      break;
    case 18:
      name = "sy";
      break;
    case 19:
      name = "sy";
      break;
    case 20:
      name = "sy";
      break;
    case 21:
      name = "sy";
      break;
    case 22:
      name = "ly";
      break;
    case 23:
      name = "zgl";
      break;
    case 24:
      name = "xc";
      break;
    case 25:
      name = "ly";
      break;
    case 26:
      name = "ly";
      break;
    case 27:
      name = "ly";
      break;
  }
  return name;
}

function paintCalendarArray(m, y) {
  var t = new Date();
  var nSpace = getMonthFirstDay(m - 1, y);
  var totalDate = getMonthTotalDate(m, y);
  var ret = [];
  var trArray = [];

  for (var i = 1;; i++) {
    if (i <= nSpace) {
      trArray.push('');
    } else if (i <= totalDate + nSpace) {
      var nl = CN_Date(y, m, i - nSpace);
      trArray.push({
        num: i - nSpace,
        isShowDayInfo: false,
        nongli: nl.slice(nl.length - 2),
        nongliInfo: CN_Date(y, m, i - nSpace),
        isToday: (m == t.getMonth() + 1 && i - nSpace == t.getDate() && y == t.getFullYear()) ? true : false,
      });
    } else {
      var l = trArray.length;
      for (var j = 0; j + l < 7; j++) {
        trArray.push('');
      }
      ret.push(trArray);
      break;
    }
    if (trArray.length == 7) {
      ret.push(trArray);
      trArray = [];
    }
  }
  return ret;
}

module.exports = {
  'getMonthFirstDay': getMonthFirstDay,
  'getMonthTotalDate': getMonthTotalDate,
  'getFromTodayDays': getFromTodayDays,
  'paintCalendarArray': paintCalendarArray,
  'webScheduling': webScheduling,
  'javaScheduling': javaScheduling,
  'androidScheduling': androidScheduling,
  'iosScheduling': iosScheduling,
  'testScheduling': testScheduling
}